/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.jubb;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2004-4-21
 * Time: 19:54:32
  */

import com.jspx.utils.XMLUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class URLFilter extends HTMLFilter
{

    public URLFilter(String s)
    {
        super(s);
    }
    public URLFilter()
    {

    }
    public String convertString()
    {
        return URLConverter();
    }
    public String URLConverter()
    {
        Pattern pattern = Pattern.compile("(\\[(link)=([^\\[]*)\\])([^\\[]*)(\\[\\/(link)\\])", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(s);
        StringBuffer stringbuffer = new StringBuffer();
        for (boolean flag = matcher.find(); flag; flag = matcher.find())
        {
            StringBuilder tempBuilder = new StringBuilder();
            tempBuilder.append("<a href=\"").append(matcher.group(3)).append("\" target=\"_blank\">").append(matcher.group(4)).append("</a>");
            matcher.appendReplacement(stringbuffer, tempBuilder.toString());
        }
        matcher.appendTail(stringbuffer);



        pattern = Pattern.compile("(\\[(URL)((=(((http://)|(ftp://))?)(.*))?)\\])(.[^\\[]*)(\\[(\\/URL)\\])", Pattern.CASE_INSENSITIVE+ Pattern.COMMENTS);


        matcher = pattern.matcher(stringbuffer.toString());
        stringbuffer.setLength(0);

        for(boolean flag = matcher.find(); flag; flag = matcher.find())
        {

            if(!matcher.group(3).equals(""))
            {
                if (isSafetyURL(matcher.group(9)))
                {
                    matcher.appendReplacement(stringbuffer, "<a href=\"");
                    if(matcher.group(5).equals(""))
                    {
                        if(matcher.group(10) == null)
                            stringbuffer.append("http://").append(matcher.group(9)).append("\" target=\"_blank\">").append(XMLUtil.escape(matcher.group(9))).append("</a>");
                        else
                            stringbuffer.append("http://").append(matcher.group(9)).append("\" target=\"_blank\">").append(XMLUtil.escape(matcher.group(10))).append("</a>");
                        continue;
                    }
                    if(matcher.group(10) == null)
                        stringbuffer.append(matcher.group(5)).append(matcher.group(9)).append("\" target=\"_blank\">").append(XMLUtil.escape(matcher.group(5))).append(matcher.group(9)).append("</a>");
                    else
                        stringbuffer.append(matcher.group(5)).append(matcher.group(9)).append("\" target=\"_blank\">").append(XMLUtil.escape(matcher.group(10))).append("</a>");

                }  else {
                    matcher.appendReplacement(stringbuffer, "<span class=\"safetyHref\">");
                    stringbuffer.append(matcher.group(5) == null ? "" : matcher.group(5)).append(matcher.group(9)==null?"":matcher.group(9)).append("(").append(XMLUtil.escape(matcher.group(10))).append(")").append("</span>");
                }
                continue;
            }
            if (isSafetyURL(matcher.group(10)))
            {
                if("".equals(matcher.group(11)) ) {
                    matcher.appendReplacement(stringbuffer, "<a href=\"");
                    stringbuffer.append("http://").append(matcher.group(10)).append("\" target=\"_blank\">").append(XMLUtil.escape(matcher.group(10))).append("</a>");
                } else
                if(!"".equals(matcher.group(11)))
                {
                    matcher.appendReplacement(stringbuffer, "<a href=\"");
                    stringbuffer.append(matcher.group(10)).append("\" target=\"_blank\">").append(XMLUtil.escape(matcher.group(10))).append("</a>");
                }
            }
            else
            {
                matcher.appendReplacement(stringbuffer, "<span class=\"safetyHref\">");
                stringbuffer.append(matcher.group(5)==null?"":matcher.group(5)).append(matcher.group(9)==null?"":matcher.group(9)).append("(").append(XMLUtil.escape(matcher.group(10))).append(")").append("</span>");
            }
        }
        matcher.appendTail(stringbuffer);
        return stringbuffer.toString();

    }
}